//index.js
//获取应用实例
var app = getApp();
//console.log(app.globalData);
Page({
  data: {
    userInfo: {},
    imageList: [],
    imageKeys: [],
    topicType: 2,
    type:'',
    groupData: {},
    topicData: {},
    videoSrc: '',
    videoKey: '',
    topic_id: 0,
    chooseVideoText: '选择视频',
    status: 0,


  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log(options);
    //options.topic_id = 83556;
    this.setData({ startYear: app.date(), startdate: app.date() })
   //this.setData({ enddate: app.date(7) })
    this.setData({ window: app.globalData.window, pid: options.pid, pid2: options.pid2 ? options.pid2 : 0, topic_id: options.topic_id, type: options.type})
    if (this.data.pid && this.data.pid>0){
      this.getGroup();
    }

    
    //console.log(this.data);
    if (this.data.topic_id > 0) {
      this.getTopic();
      wx.setNavigationBarTitle({
        title: '修改'
      });
    }
    app.getUserInfo((userInfo) => {
      this.data.userInfo = userInfo;
    })
    //this.getTopicList();
  },


  getTopic: function () {
    console.log("getTopic");
    var that = this;
    var pid = this.data.pid2;
    var topic_id = this.data.topic_id;
    var id = topic_id ? topic_id : pid;
    app.api("sns/topic/detail", {
      id: id,
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
     // that.getPidArray();
      that.setData({ topicData: res.data, pid: res.data.pid, groupData: res.data.group });
    
      if (topic_id > 0) {
        console.log(res.data.group);
        that.setData({
          imageList: res.data.pics,
          imageKeys: res.data.imageKeys,
          videoSrc: res.data.video_url,
          videoKey: res.data.videoKey,
          content: res.data.title,
          cate_id: res.data.cate_id,
          type:res.data.type,
          pid: res.data.pid,
          pid2: res.data.pid2,
        
        });

        if (res.data.pid2 > 0) {
          that.setData({ topicType: 1 });
        }
        
         
        if (res.data.join_count > 0) {
          that.setData({ canEditSign: 0 });
        }
        if (res.data.content && res.data.content.length > 0) {
          var contentData = JSON.parse(res.data.content);
          that.setData(contentData)
        }
      }
    })
  },


  //
  getGroup: function () {
    var that = this;
    var pid = this.data.pid;
    app.api("sns/group/find", {
      id: pid
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
     // console.log(res.data);
      that.setData({ groupData: res.data ,type:res.data.type});
      wx.setNavigationBarTitle({
        title: res.data.content.issuePageTitle
      });
    })
  },
  delImgVideo: function () {
    this.setData({
      videoSrc: '',
      videoKey: '',
    })
  },
  choose: function () {
    var that = this;
    if (this.data.videoSrc.length > 0 && this.data.imageKeys.length == 0) {
      that.chooseImage();
      return;
    }
    if (this.data.imageKeys.length > 1) {
      that.chooseImage();
      return;
    }

    wx.showActionSheet({
      itemList: ['上传图片', '上传视频'],
      success: function (res) {
        if (res.tapIndex == 0) {
          that.chooseImage();
        } else if (res.tapIndex == 1) {
          that.chooseVideo();
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  chooseVideo: function () {
    var that = this;
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      success: function (res) {
        var filePath = res.tempFilePath;
        that.setData({ chooseVideoText: '上传中...' });
        app.upload(filePath, (res) => {
          console.log(res);
          that.setData({ chooseVideoText: '选择视频' });
          that.setData({
            videoSrc: res.imageURL,
            videoKey: res.key,
          })
        })

      }
    })
  },

  chooseImage: function () {
    var that = this
    var imageList = this.data.imageList;
    var imageKeys = this.data.imageKeys;
    // if (this.data.topicType==2){
    //   var max = 3 - this.data.imageList.length;
    // }else{
    //   var max =1;
    // }
    var max = 3;
    //上传了视频那只能上传一张图片 
    if (this.data.videoSrc.length > 0) {
      var max = 1;
    }
    wx.chooseImage({
      sizeType: 'compressed',
      count: max,
      success: function (res) {
        var newImages = res.tempFilePaths;
        for (var i in newImages) {
          var filePath = newImages[i];
          app.upload(filePath, (res) => {
            imageList = imageList.concat(res.imageURL);
            imageKeys = imageKeys.concat(res.key);
            that.setData({ imageList: imageList, imageKeys: imageKeys });
          })
        }
      }
    })
  },
  delImg: function (e) {
    var id = e.currentTarget.dataset.id;
    var imageList = this.data.imageList;
    var imageKeys = this.data.imageKeys;
    imageList.splice(id, 1);
    imageKeys.splice(id, 1);
    this.setData({ imageList: imageList, imageKeys: imageKeys });
  },
  //浏览图片
  previewImage: function (e) {
    var current = e.target.dataset.src
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },
  //发布活动 
  formSubmit: function (e) {
    var formId = e.detail.formId;
    var user_info = app.globalData.userInfo;
    var postData = e.detail.value;
    if (this.data.loading == 1) {
      return;
    }
    
      if (postData.title == "" && this.data.imageKeys.length == 0 && this.data.videoKey.length == 0) {
        app.alert('请先添加视频/文字/图片,其中一种内容');
        return false;
      }
    

    var groupData = this.data.groupData;
 
    this.setData({ 'loading': 1 });
    this.setData({ 'disabled': 1 });
    var _this = this;
    var title = postData.title;

    //提交回答
    app.api("sns/topic/add", {
      title: title,
      formId: formId,
      desc: postData.desc,
      pics: this.data.imageKeys.join(","),
      type: this.data.type,
      pid: this.data.pid,
      id: this.data.topic_id,
      pid2: this.data.pid2,
      video_url: this.data.videoKey,
      // cate_id: this.data.cate_id,
      method: 'POST',
    }, function (res) {
      _this.setData({ 'loading': 0 });
      _this.setData({ 'disabled': 0 });
      var title = "";
      if (res.code == 200) {
        // wx.showToast({
        //   title: '发布成功',
        //   icon: 'success',
        //   duration: 2000
        // });

        var topic_id = res.data.id;
        _this.suc();
        // if (!_this.data.signIn) {//心愿单
        //   _this.join_topic(topic_id);
        // } else {
        //   _this.suc();
        // }
        // 
        //  
        // _this.navigateBack();
      } else {
        app.alert(res.msg);
      }
    })
  },


  suc: function () {
    var _this = this;
    wx.showToast({
      title: '发布成功',
      icon: 'success',
      duration: 2000
    });
    app.setPageData("group/index", { reload: 1 });
    _this.navigateBack();
  },
  //返回 
  navigateBack: function (e) {
    wx.navigateBack({
      delta: 1
    })
  },
 



})
